package com.sg.business.management.ui.page;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.IManagedForm;

import com.mobnut.db.model.PrimaryObject;
import com.sg.bpm.workflow.model.DroolsProcessDefinition;
import com.sg.bpm.workflow.model.NodeAssignment;
import com.sg.business.commons.ui.flow.ProcessSettingPanel;
import com.sg.business.commons.ui.flow.ProcessSettingPanel.IProcessSettingListener;
import com.sg.business.commons.ui.page.flow.AbstractWorkdProcessPage;
import com.sg.business.management.ui.page.WFDurationEditor.ITaskDurationSettingListener;
import com.sg.business.model.AbstractRoleDefinition;
import com.sg.business.model.IProcessControl;
import com.sg.business.model.User;
import com.sg.business.model.WFHumanTaskSetting;
import com.sg.business.model.WorkDefinition;
import com.sg.widgets.part.editor.PrimaryObjectEditorInput;
import com.sg.widgets.part.editor.page.AbstractFormPageDelegator;
import com.sg.widgets.registry.config.BasicPageConfigurator;

public class WFDurationPage extends AbstractFormPageDelegator implements
		IProcessSettingListener, ITaskDurationSettingListener {

	private WFDurationSettingPanel wfDurationSettingPanel;

	@Override
	public Composite createPageContent(IManagedForm mForm, Composite parent,
			PrimaryObjectEditorInput input, BasicPageConfigurator conf) {
		parent.setBackgroundMode(SWT.INHERIT_DEFAULT);
		setFormInput(input);
		IProcessControl iProcessControl = getIProcessControl();
		DroolsProcessDefinition processDefinition = iProcessControl
				.getProcessDefinition(getProcessKey());
		wfDurationSettingPanel = new WFDurationSettingPanel(parent, input);
		wfDurationSettingPanel.setValue(processDefinition);
		wfDurationSettingPanel.addValueChangeListener(this);
		IFormPart[] parts = mForm.getParts();
		for (int i = 0; i < parts.length; i++) {
			IFormPart part = parts[i];
			if (part instanceof AbstractWorkdProcessPage) {
				AbstractWorkdProcessPage abstractWorkdProcessPage = (AbstractWorkdProcessPage) part;
				ProcessSettingPanel processSettingPanel = abstractWorkdProcessPage
						.getProcessSettingPanel();
				processSettingPanel.addProcessSettingListener(this);
			}
		}
		return wfDurationSettingPanel;
	}

	@Override
	public void commit(boolean onSave) {
		if (onSave) {
			setDirty(false);
		}
	}

	@Override
	public void setFocus() {

	}

	@Override
	public void actorChanged(User newActor, User oldActor,
			NodeAssignment nodeAssignment, AbstractRoleDefinition roleDef) {

	}

	@Override
	public void processChanged(DroolsProcessDefinition newProcessDefinition,
			DroolsProcessDefinition oldProcessDef) {
		wfDurationSettingPanel.setValue(newProcessDefinition);
	}

	@Override
	public void processActivatedChanged(boolean activated) {

	}

	@Override
	public void roleChanged(AbstractRoleDefinition newRole,
			AbstractRoleDefinition oldRole, NodeAssignment nodeAssignment) {

	}

	protected IProcessControl getIProcessControl() {
		PrimaryObjectEditorInput input = getInput();
		PrimaryObject po = input.getData();
		return (IProcessControl) po.getAdapter(IProcessControl.class);
	}

	protected String getProcessKey() {
		return WorkDefinition.F_WF_EXECUTE;
	}

	@Override
	public void activiteValueChanged(WFHumanTaskSetting newValue) {
		setDirty(true);
	}
}
